<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
            v-model="queryParams.modelName"
            placeholder="请输入模型名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="光纤通道" prop="fiberNum">
        <el-select v-model="queryParams.fiberNum" placeholder="请选择光纤通道编号" clearable>
          <el-option
              v-for="dict in fiber_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['ba:baModel2:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['ba:baModel2:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['ba:baModel2:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['ba:baModel2:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baModel2List" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="模型id" align="center" prop="modelId" />
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <el-table-column label="模型长度(m)" align="center" prop="modelLength" />
      <el-table-column label="光纤通道编号" align="center" prop="fiberNum">
        <template #default="scope">
          <dict-tag :options="fiber_info" :value="scope.row.fiberNum"/>
        </template>
      </el-table-column>
      <el-table-column label="光纤半径" align="center" prop="pipeRadius" />
      <el-table-column label="线路a起点位置" align="center" prop="pipeA1" />
      <el-table-column label="线路a终点位置" align="center" prop="pipeA2" />
      <el-table-column label="线路b起点位置" align="center" prop="pipeB1" />
      <el-table-column label="线路b终点位置" align="center" prop="pipeB2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ba:baModel2:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ba:baModel2:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改崩岸检测模型2芯对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="baModel2Ref" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="模型id" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入模型id" />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="模型长度" prop="modelLength">
          <el-input v-model="form.modelLength" placeholder="请输入模型长度" />
        </el-form-item>
        <el-form-item label="光纤通道编号" prop="fiberNum">
          <el-select v-model="form.fiberNum" placeholder="请选择光纤通道编号">
            <el-option
                v-for="dict in fiber_info"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="光纤半径" prop="pipeRadius">
          <el-input v-model="form.pipeRadius" placeholder="请输入光纤半径" />
        </el-form-item>
        <el-form-item label="线路a起点位置" prop="pipeA1">
          <el-input v-model="form.pipeA1" placeholder="请输入线路a起点位置" />
        </el-form-item>
        <el-form-item label="线路a终点位置" prop="pipeA2">
          <el-input v-model="form.pipeA2" placeholder="请输入线路a终点位置" />
        </el-form-item>
        <el-form-item label="线路b起点位置" prop="pipeB1">
          <el-input v-model="form.pipeB1" placeholder="请输入线路b起点位置" />
        </el-form-item>
        <el-form-item label="线路b终点位置" prop="pipeB2">
          <el-input v-model="form.pipeB2" placeholder="请输入线路b终点位置" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BaModel2">
import { listBaModel2, getBaModel2, delBaModel2, addBaModel2, updateBaModel2 } from "@/api/ba/baModel2";

const { proxy } = getCurrentInstance();
const { fiber_info } = proxy.useDict('fiber_info');

const baModel2List = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    modelName: null,
    fiberNum: null,
  },
  rules: {
    modelId: [
      { required: true, message: "模型id不能为空", trigger: "blur" }
    ],
    modelName: [
      { required: true, message: "模型名称不能为空", trigger: "blur" }
    ],
    modelLength: [
      { required: true, message: "模型长度不能为空", trigger: "blur" }
    ],
    fiberNum: [
      { required: true, message: "光纤通道编号不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询崩岸检测模型2芯列表 */
function getList() {
  loading.value = true;
  listBaModel2(queryParams.value).then(response => {
    baModel2List.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    modelId: null,
    modelName: null,
    modelLength: null,
    fiberNum: null,
    pipeRadius: null,
    pipeA1: null,
    pipeA2: null,
    pipeB1: null,
    pipeB2: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("baModel2Ref");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加崩岸检测模型2芯";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBaModel2(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改崩岸检测模型2芯";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["baModel2Ref"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBaModel2(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBaModel2(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除崩岸检测模型2芯编号为"' + _ids + '"的数据项？').then(function() {
    return delBaModel2(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ba/baModel2/export', {
    ...queryParams.value
  }, `baModel2_${new Date().getTime()}.xlsx`)
}

getList();
</script>
